package com.imooc.controller;

import com.github.pagehelper.PageHelper;
import com.imooc.common.utils.common.lang.StringUtils;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.VO.*;
import com.imooc.service.ItemsService;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lihongyu
 * @CreateTime 2021-10-13 13:55
 * @Description 商品相关接口
 */
@Api(value = "商品相关接口",tags = "商品相关接口")
@RestController
@RequestMapping("/items")
public class ItemsController extends BaseController{

    @Resource
    private ItemsService itemsService;

    /**
     * 根据商品ID获取商品详情信息
     * @param itemId 商品ID
     * @return 商品详情信息
     */
    @GetMapping("/info/{itemId}")
    @ApiOperation(value = "根据商品ID获取商品详情信息", notes = "根据商品ID获取商品详情信息", httpMethod = "GET")
    @ApiImplicitParam(name = "itemId",value = "商品ID",paramType = "path")
    public IMOOCJSONResult getItemsInfoById(@PathVariable String itemId){
        if(StringUtils.isBlank(itemId)){
            return IMOOCJSONResult.errorMap("");
        }
        Items items = itemsService.getItemsById(itemId);
        List<ItemsImg> itemsImgs = itemsService.getItemsImgListByItemId(itemId);
        List<ItemsSpec> itemsSpecList = itemsService.getItemsSpecListByItemId(itemId);
        ItemsParam itemsParam = itemsService.getItemsParamsByItemId(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgs);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);
        return IMOOCJSONResult.ok(itemInfoVO);
    }

    /**
     * 获取商品评价等级总记录数
     *
     * @param itemId 商品id
     * @return 商品评价等级总记录数
     */
    @GetMapping("/commentLevel")
    @ApiOperation(value = "获取商品评价等级总记录数", notes = "获取商品评价等级总记录数", httpMethod = "GET")
    @ApiImplicitParam(name = "itemId",value = "商品ID",paramType = "query")
    public IMOOCJSONResult getCommentLevelCountByItemId(@RequestParam String itemId){
        if(StringUtils.isBlank(itemId)){
            return IMOOCJSONResult.errorMap("");
        }
        ItemsCommentsLevelCountVO itemsCommentsLevelCountVO = itemsService.getItemsCommentLevelCountByItemId(itemId);
        return IMOOCJSONResult.ok(itemsCommentsLevelCountVO);
    }

    /**
     * 获取商品评价列表
     *
     * @param itemId 商品id
     * @param level 评价等级
     * @return 商品评价等级总记录数
     */
    @GetMapping("/comments")
    @ApiOperation(value = "获取商品评价等级总记录数", notes = "获取商品评价等级总记录数", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId",value = "商品ID",paramType = "query"),
            @ApiImplicitParam(name = "level",value = "评价等级",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面容量",paramType = "query")
    })
    public IMOOCJSONResult getCommentLevelCountByItemId(@RequestParam String itemId,
                                                        @RequestParam Integer level,
                                                        @RequestParam(required = false,defaultValue = "1")Integer page,
                                                        @RequestParam(required = false,defaultValue = "10")Integer pageSize){
        if(StringUtils.isBlank(itemId)){
            return IMOOCJSONResult.errorMap("");
        }
        PageHelper.startPage(page, pageSize);
        List<ItemCommentVO> itemCommentVOList = itemsService.getItemsCommentListByItemIdAndCommentLevel(itemId,level);
        return IMOOCJSONResult.ok(setterPagedGrid(itemCommentVOList,page));
    }

    /**
     * 搜索商品接口
     *
     * @param keywords 搜索关键词
     * @param sort 排序字段
     * @return 搜索结果商品列表
     */
    @GetMapping("/search")
    @ApiOperation(value = "搜索商品接口", notes = "搜索商品接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keywords",value = "搜索关键词",paramType = "query"),
            @ApiImplicitParam(name = "sort",value = "排序字段",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面容量",paramType = "query")
    })
    public IMOOCJSONResult searchItems(@RequestParam String keywords,
                                            @RequestParam(required = false) String sort,
                                            @RequestParam(required = false,defaultValue = "1")Integer page,
                                            @RequestParam(required = false,defaultValue = "20")Integer pageSize){
        if(StringUtils.isBlank(keywords)){
            return IMOOCJSONResult.errorMap("");
        }
        PageHelper.startPage(page, pageSize);
        List<SearchItemVO> searchItemVOList = itemsService.searchItemsList(keywords,sort);
        return IMOOCJSONResult.ok(setterPagedGrid(searchItemVOList,page));
    }

    /**
     * 根据分类Id搜索商品接口
     *
     * @param catId 分类id
     * @param sort 排序字段
     * @return 搜索结果商品列表
     */
    @GetMapping("/catItems")
    @ApiOperation(value = "根据分类Id搜索商品接口", notes = "根据分类Id搜索商品接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catId",value = "分类id",paramType = "query"),
            @ApiImplicitParam(name = "sort",value = "排序字段",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面容量",paramType = "query")
    })
    public IMOOCJSONResult catItems(@RequestParam String catId,
                                            @RequestParam(required = false) String sort,
                                            @RequestParam(required = false,defaultValue = "1")Integer page,
                                            @RequestParam(required = false,defaultValue = "20")Integer pageSize){
        if(StringUtils.isBlank(catId)){
            return IMOOCJSONResult.errorMap("");
        }
        PageHelper.startPage(page, pageSize);
        List<SearchItemVO> searchItemVOList = itemsService.searchItemsListByCatId(catId,sort);
        return IMOOCJSONResult.ok(setterPagedGrid(searchItemVOList,page));
    }

    /**
     * 根据商品规格ids查找最新的商品数据
     * 用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格），类似京东淘宝
     *
     * @param itemSpecIds 拼接的规格ids
     * @return 购物车商品信息
     */
    @GetMapping("/refresh")
    @ApiOperation(value = "根据商品规格ids查找最新的商品数据", notes = "根据商品规格ids查找最新的商品数据", httpMethod = "GET")
    @ApiImplicitParam(name = "itemSpecIds",value = "拼接的规格ids",paramType = "query",example = "1001,1003,1005")
    public IMOOCJSONResult refresh(@RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return IMOOCJSONResult.ok();
        }
        List<ShopcartVO> list = itemsService.queryItemsBySpecIds(itemSpecIds);
        return IMOOCJSONResult.ok(list);
    }

}
